var url = "/openx/order/businessConfig/listBusinessConfig";
var queryForm = 'queryForm';

var ownOptions={};
/**查询参数信息*/
//对应接口入参
var param = {};

var formId = 'queryForm';

//匹配门店编码的正则表达式

var storeCodeRegex = /^(\d{4})(,\d{4})*$/;

(function () {
//        /**设置增删改查的工具条*/
    $.openx.setTableToolBar('partnerbar');
    /**进行表格数据初始化*/
    var data = $.openx.getJsonObj(formId);
    data.businessType = 'PICK_HINT_TIME';
    param.businessConfig = data;
    $.openx.initTable("table", url, param);
})();

$.openx.insert = function () {

    var createForm = "addBusinessConfigForm";
    var innerHtml = $.openx.innerHtml('addBusinessConfigDialog');
    laytpl(innerHtml).render({}, function (html) {
        $.openx.openLayer(['auto', '200'], '新增拣货下账提示时间段管理', html, false, function () {
        	var param = $.openx.getJsonObj(createForm);
        	if ($.openx.validation(createForm)) {
                //获取表单数据
        		if(param.startHous > param.endHous ){
        			layer.tips('开始小时不能不能大于结束小时', '#startHous', {
                        tips: [2, '#ff4400']
        			});
        			return false;
        		}
        		if(param.startHous == param.endHous && param.startMin > param.endMin){
        			layer.tips('同一小时时，开始分支数不能大于结束分钟数', '#startMin', {
        				tips: [2, '#ff4400']
        			});
        			return false;
        		}
        		param.dataValue = param.startHous+":"+param.startMin  + "-" +param.endHous+":"+param.endMin;
                param.businessType = 'PICK_HINT_TIME';
                console.log(param);
                $X("/order/businessConfig", "saveBusinessConfig").callx({
                	businessConfig: param,
                    onResult: function (data) {
                        if (data) {
                            $.openx.tableRefresh(null, null, true);
                            layer.closeAll();
                            layer.msg("新增成功");
                        }
                    },
                    onError: function (error) {
                        var res = JSON.parse(error);
                        layer.msg(res.message)
                    }
                });
            }
        });
        initTimeSelect("startHous","startMin","endHous","endMin");
    });

}


function createTimeFormat(value, row) {
	if(value){
    	return $.openx.formatDate(new Date(value));
	}
}

function initTimeSelect(startHousId,startMinId,endHousId,endMinId){
	
	var housHtml = "";
	var minHtml = "";
	for (var i = 0; i <= 23; i++) {
		housHtml += "<option value='"+(i<10?"0"+i:i)+"'>"+(i<10?"0"+i:i)+"</option>";
	}
	for (var i = 0; i <= 59; i++) {
		minHtml += "<option value='"+(i<10?"0"+i:i)+"'>"+(i<10?"0"+i:i)+"</option>";
	}
	$("#"+startHousId).html(housHtml);
	$("#"+endHousId).html(housHtml);
	$("#"+startMinId).html(minHtml);
	$("#"+endMinId).html(minHtml);
}

$.openx.update = function () {
    $.openx.onClickUpdate(function (row) {
    	
        var updateForm = "addBusinessConfigForm";
        var innerHtml = $.openx.innerHtml('addBusinessConfigDialog');
        laytpl(innerHtml).render(row[0], function (html) {
            $.openx.openLayer(['auto', '200'], '修改拣货下账提示时间段管理', html, false, function () {
            	var param = $.openx.getJsonObj(updateForm);
            	if(param.startHous > param.endHous ){
        			layer.tips('开始小时不能不能大于结束小时', '#startHous', {
                        tips: [2, '#ff4400']
        			});
        			return false;
        		}
        		if(param.startHous == param.endHous && param.startMin > param.endMin){
        			layer.tips('同一小时时，开始分支数不能大于结束分钟数', '#startMin', {
        				tips: [2, '#ff4400']
        			});
        			return false;
        		}
        		param.dataValue = param.startHous+":"+param.startMin  + "-" +param.endHous+":"+param.endMin;
                param.businessType = 'PICK_HINT_TIME';
                console.log(param);
                $X("/order/businessConfig", "modifyBusinessConfig").callx({
                	businessConfig: param,
                    onResult: function (data) {
                        if (data) {
                            $.openx.tableRefresh(null, null, true);
                            layer.closeAll();
                            layer.msg("更新成功");
                        }
                    },
                    onError: function (error) {
                        var res = JSON.parse(error);
                        layer.msg(res.message)
                    }
                });

              });
           });
        var times = row[0].dataValue.split("-");
        initTimeSelect("startHous","startMin","endHous","endMin");
        $("#startHous").val(times[0].split(":")[0]);
        $("#startMin").val(times[0].split(":")[1]);
        $("#endHous").val(times[1].split(":")[0]);
        $("#endMin").val(times[1].split(":")[1]);
        $("#add_id").val(row[0].id);
//        $("#startTime").val(times[0]);
//        $("#endTime").val(times[1]);
        
      });
};




//删除操作
$.openx.delete = function () {
    $.openx.onClickDelete(function (row) {
    	var ids = $.openx.getArrays('id',row);
        $X("/order/businessConfig", "deleteBusinessConfig").callx({
            ids: ids,
            onResult: function (data) {
            	if(data) {
	                $.openx.tableRefresh(null, null, true);
	                layer.msg("删除成功");
            	}
            },
            onError: function (error) {
                layer.msg("系统错误，请联系管理员");
            }
        });
    });
};


$.openx.query = function () {
    var data = $.openx.getJsonObj(queryForm);
    param.bean = data;
    $.openx.tableRefresh(url, param);

}